import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:yuellyfactory/components/ParameterDialog.dart';
import 'package:yuellyfactory/util/ColorUtil.dart';
import 'package:yuellyfactory/views/dto/Parameters.dart';
import 'package:yuellyfactory/views/dto/PricePart.dart';

class ViewUtil{

  static double matchWidth(BuildContext context){
   // var width=MediaQuery.of(context).size.width;
    var width=ScreenUtil().screenWidth;

    return width;
  }
  static double matchHeight(BuildContext context){
    // return MediaQuery.of(context).size.height;
    return ScreenUtil().screenHeight;
  }

  static double screenWidth(double width){
    return ScreenUtil().setWidth(width);
  }
  static double screenHeight(double width){
    return ScreenUtil().setHeight(width);
  }

  static double fontSize(double size){

  //  return ScreenUtil().setSp(size);
    return size*0.65;
  }

  static double radio(double size){
    return ScreenUtil().radius(size);
  }

  static Widget lineView(){
    return Container(
      color:ColorUtil.colorGray,
      margin: EdgeInsets.only(top: ViewUtil.screenHeight(15)),
      height: ViewUtil.screenHeight(1),
    );
  }

  static String showImgUrl(String url){
    if(url.startsWith("http")){
      return url;
    }
    return "http://39.108.10.224:9091/file/yuelly/$url";
  }

  static void hideKeyBoard(){
    FocusManager.instance.primaryFocus?.unfocus();
  }

  static Widget defalutWhiteBg(BuildContext context){
    return Container(
      color: Colors.white,
      width: ViewUtil.matchWidth(context),
      height: ViewUtil.matchHeight(context),
    );
  }

  static Widget defalutGrayBg(BuildContext context){
    return Container(
      color: const Color(0xffF2F3F7),
      width: ViewUtil.matchWidth(context),
      height: ViewUtil.matchHeight(context),
    );
  }


  static Widget defaultTopBg(BuildContext context){
    return Container(
      width: ViewUtil.matchWidth(context),
      height: ViewUtil.screenHeight(500),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [
            Color(0xffC5E2FA),
            Color(0xffffffff)
          ],
        ),
      ),
    );

  }

  static AppBar defaultAppBar(String title,VoidCallback callback){
    return AppBar(

      leading: IconButton(icon: const Icon(Icons.arrow_back_ios_new),color: const Color(0xff333333),
        iconSize: ViewUtil.screenWidth(90),onPressed: (){
          callback();
        },),
      backgroundColor: Colors.transparent,
      shadowColor: Colors.transparent,
      centerTitle: true,
      title:  Text(title,
        style: TextStyle(color:const  Color(0xff131416),fontSize: ViewUtil.fontSize(54)),),
    );
  }
  static AppBar defaultAppBarWithAction(String title,List<Widget> actions,VoidCallback callback){
    return AppBar(

      leading: IconButton(icon: const Icon(Icons.arrow_back_ios_new),color: const Color(0xff333333),
        iconSize: ViewUtil.screenWidth(90),onPressed: (){
          callback();
        },),
      actions: actions,
      backgroundColor: Colors.transparent,
      shadowColor: Colors.transparent,
      centerTitle: true,
      title:  Text(title,
        style: TextStyle(color:const  Color(0xff131416),fontSize: ViewUtil.fontSize(54)),),
    );
  }


  static Widget textButton(String text,double paddingLeft,double paddingTop,
      double radius,
      Color backGroundColor,Color borderColor,
      Color textColor,double textSize,VoidCallback callback) {
    return TextButton(
      style: ButtonStyle(
        backgroundColor:MaterialStateProperty.all<Color>(backGroundColor),
        shape: MaterialStateProperty.all(RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radius))),
        minimumSize: MaterialStateProperty.all<Size>(const Size(30, 20)),
        side: MaterialStateProperty.all(
          BorderSide(width: ViewUtil.screenWidth(1), color: borderColor),
        ),
        padding:MaterialStateProperty.all<EdgeInsets>(EdgeInsets.only(left: paddingLeft,right: paddingLeft,top: paddingTop,bottom: paddingTop)),  ),
      onPressed: callback,
      child: Text(text,style: TextStyle(color: textColor,fontSize: textSize),),

    );
  }


  static Widget iconButton(IconData iconData,Color color,double size ,VoidCallback callback){

    return IconButton(icon: Icon(iconData),color:color,
      iconSize: size,onPressed: (){
        callback();
      },);
  }

  static Widget iconNormalButton(IconData iconData,Color color,double size ,VoidCallback callback){

    return IconButton(icon: Icon(iconData),color:color,
      padding: EdgeInsets.zero, // 去掉内边距（默认有8px内边距）
      splashRadius: size, // 水波
      constraints: const BoxConstraints(),
      iconSize: size,onPressed: (){
        callback();
      },);
  }


  static Widget editIcon(String tips,VoidCallback callback){
    return ShadTooltip(
      builder: (context) =>  Text(tips),
      child: iconButton(Icons.note_alt_outlined, ColorUtil.colorBlue,  25, (){
        callback();
      }) ,
    );
  }

  static Widget configIcon(String tips,VoidCallback callback){
    return ShadTooltip(
      builder: (context) =>  Text(tips),
      child: iconButton(Icons.settings, ColorUtil.colorBlue,  25, (){
        callback();
      }) ,
    );
  }
  static Widget doneIcon(String tips,VoidCallback callback){
    return ShadTooltip(
      builder: (context) =>  Text(tips),
      child: GestureDetector(onTap: (){callback();},child: Image.asset('images/submit.png',width: 35,),)
    );
  }

  static Widget logoutIcon(String tips,VoidCallback callback){
    return ShadTooltip(
      builder: (context) =>  Text(tips),
      child: iconButton(Icons.logout, ColorUtil.colorBlue,  25, (){
        callback();
      }) ,
    );
  }


  static Widget addIcon(String tips,VoidCallback callback){
    return
      ShadTooltip(
        builder: (context) =>  Text(tips),
        child: iconButton(Icons.add_circle_outline, ColorUtil.colorBlue,  25, (){
          callback();
        }) ,
      );


  }

  static Widget downIcon(String tips,VoidCallback callback){
    return
      ShadTooltip(
        builder: (context) =>  Text(tips),
        child: iconNormalButton(Icons.arrow_downward, ColorUtil.colorBlue,  15, (){
          callback();
        }) ,
      );
  }

  static Widget upIcon(String tips,VoidCallback callback){
    return
      ShadTooltip(
        builder: (context) =>  Text(tips),
        child: iconNormalButton(Icons.arrow_upward, ColorUtil.colorBlue,  15, (){
          callback();
        }) ,
      );
  }

  static Widget importIcon(String tips,VoidCallback callback){
    return
      ShadTooltip(
        builder: (context) =>  Text(tips),
        child: iconButton(Icons.import_export, ColorUtil.colorBlue,  30, (){
          callback();
        }) ,
      );


  }

  static Widget delIcon(String tips,VoidCallback callback){


    return  ShadTooltip(
      builder: (context) =>  Text(tips),
      child: iconButton(Icons.delete, ColorUtil.colorRed,  25, (){
        callback();
      }) ,
    );
  }

  static void showParmSheet(BuildContext context,String title,List<Parameters> datas){
    showShadSheet(
      barrierColor: Colors.transparent,
      side: ShadSheetSide.right,
      context: context,
      builder: (context) =>  ParameterDialog(title: title, datas: datas)

    );
  }

  static Widget bgBlock(Color bgColor,double width,double height,double radius){
    return Container(
      decoration: BoxDecoration(
        color:bgColor,
        borderRadius: BorderRadius.circular(radius),
      ),
        width: width,
        height: height,
    );
  }


  //查询面包屑列表
  static List crumbList(String id,List allDatas){
    List list=[];
    bool hasBreak=false;
    for(PricePart type in allDatas){
      if(hasBreak){
        break;
      }
      if(id==type.id){
        list.add(type);
        hasBreak=true;
        break;
      }
      for(PricePart brand in type.materialList){
        if(hasBreak){
          break;
        }
        if(id==brand.id){
          list.add(type);
          list.add(brand);
          hasBreak=true;
          break;
        }
        for(PricePart serial in brand.materialList){
          if(hasBreak){
            break;
          }
          if(id==serial.id){
            list.add(type);
            list.add(brand);
            list.add(serial);
            hasBreak=true;
            break;
          }
          for(PricePart part in serial.materialList){
            if(id==part.id){
              list.add(type);
              list.add(brand);
              list.add(serial);
              list.add(part);
              hasBreak=true;
              break;
            }


          }
        }

      }
    }
    return list;
  }

  //查询面包屑列表
  static List crumbModelList(String id,List allDatas){
    List list=[];
    bool hasBreak=false;
    for(PricePart type in allDatas){
      if(hasBreak){
        break;
      }
      if(id==type.id){
        list.add(type);
        hasBreak=true;
        break;
      }
      for(PricePart brand in type.priceModels){
        if(hasBreak){
          break;
        }
        if(id==brand.id){
          list.add(type);
          list.add(brand);
          hasBreak=true;
          break;
        }
        for(PricePart serial in brand.priceModels){
          if(hasBreak){
            break;
          }
          if(id==serial.id){
            list.add(type);
            list.add(brand);
            list.add(serial);
            hasBreak=true;
            break;
          }
          for(PricePart part in serial.priceModels){
            if(id==part.id){
              list.add(type);
              list.add(brand);
              list.add(serial);
              list.add(part);
              hasBreak=true;
              break;
            }


          }
        }

      }
    }
    return list;
  }


  static List<String> picStringToList(String pic){
    if(pic==null||pic.isEmpty)return [];
    List<String> list=pic.split(";");
    return list;
  }

  static String picListToString(List<String> list){
    String str="";
    for(var i=0;i<list.length;i++){
      str=str+list[i];
      if(i!=list.length-1){
        str="$str;";
      }
    }

    return str;
  }
}